<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" title="入库" width="50%" append-to-body>
      <div>
        <div class="dialog-content">
          <div class="dialog-main">
            <el-form label-width="100" ref="form" :model="formData" :rules="rules">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="applyData.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="Plus"
                    @click="handleAdd"
                >新增</el-button>
              </el-col>
            </el-row>
            <el-form :model="tableData">
              <el-table v-loading="tableData.loading" :data="tableData.list">

                <el-table-column label="名称" prop="name">
                  <template #default="scope">
                    <el-form-item :prop="'list.'+ scope.$index + '.name'" :rules="rules.name">
                      <el-input placeholder="请输入" v-model="scope.row.name"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>

                <el-table-column label="类别" prop="categoryId">
                  <template #default="scope">
                    <category-select v-model="scope.row.categoryId"></category-select>
                  </template>
                </el-table-column>

                <el-table-column label="品牌" prop="brand">
                  <template #default="scope">
                    <el-input placeholder="请输入" v-model="scope.row.brand"></el-input>
                  </template>
                </el-table-column>

                <el-table-column label="型号" prop="model">
                  <template #default="scope">
                    <el-input placeholder="请输入" v-model="scope.row.model"></el-input>
                  </template>
                </el-table-column>

                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template  #default="scope">
                    <el-link
                        size="primary"
                        @click="handleDelete(scope)"
                    >删除</el-link>

                  </template>
                </el-table-column>
              </el-table>
            </el-form>
          </div>
          <div class="dialog-aside">
            <RFIDBlock ref="rfidBlockRef"></RFIDBlock>
          </div>
        </div>
      </div>



      <div slot="footer" class="dialog-footer">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="sub">提交</el-button>
          <el-button @click="close">取 消</el-button>
        </el-row>

      </div>
    </el-dialog>
  </div>
</template>

<script setup>

import api from "@/api/asset/apply.js";

import CategorySelect from "@/components/Asset/CategorySelect.vue";
import RFIDBlock from "@/components/RFID/Block.vue"

const { proxy } = getCurrentInstance();

const emit = defineEmits(['sub']);
const rfidBlockRef = ref();

const props = defineProps({
  applyType: {
    type: String,
    required: true,
  }
})

// 是否编辑
const isEdit = ref(false);
const rules = {
  name: {
    required: true, message: '不能为空'
  }
}

const applyDataInit = {
  id: null,
  applyType: props.applyType,
  remark: null
}

const applyData = reactive(applyDataInit);

const formDataInit = {}

let itemsDel = [];

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
});


const openFlag = ref(false);
const form = ref();

const emptyItem = {
  number: null,
  num: 1,
}

const formData = reactive(formDataInit)

function reset() {
  Object.assign(formData, formDataInit);
  tableData.list = [];
  itemsDel = [];
}

function open(row) {
  openFlag.value = true;
  reset();
  if (row) {
    api.detail(row).then(response=> {
      Object.assign(applyData, response);
      const resFormData = response.formData;
      if (resFormData) {
        Object.assign(formData, JSON.parse(resFormData));
      }
      tableData.list = response.relItems;
      itemsDel = [];

    })
    isEdit.value = true;
  } else {
    isEdit.value = false;
  }
  nextTick(() => {
    rfidBlockRef.value.start();
  });

}

function close() {
  openFlag.value = false;
}

function onClose() {
  rfidBlockRef.value.stop();
}

function handleAdd() {
  tableData.list.push({...emptyItem});
}

function handleDelete({ row, $index }) {
  // 如果是编辑状态需要把删除的行存起来
  if (isEdit.value) {
    itemsDel.push(row);
  }
  tableData.list.splice($index, 1);
}

function getParams() {
  const apply = { ...applyData };
  apply.applyType = props.applyType;
  apply.formData = JSON.stringify(formData);
  return {
    edit: isEdit.value,
    apply: apply,
    items: tableData.list,
    itemsDel: itemsDel
  }
}

async function check() {
  if (!tableData.list.length) {
    proxy.$modal.alert('入库的资产不能为空');
    return false;
  }
  return await form.value.validate();
}

/** 保存 */
async function save() {
  let checkResult = await check();
  debugger
  if (!checkResult) {
    return false;
  }
  api.save(getParams()).then(response => {
    emit("sub");
    close();
  });
}

function sub() {
  check().then(()=> {
    api.saveAndSub(getParams()).then(response => {
      emit("sub");
      close();
    });
  });

}

defineExpose({
  open
})

</script>

<style lang="scss">

.el-table {
  .el-form-item {
    margin-bottom: 0;
  }
}

.el-form-item__error {
  top: 40%;
  left: 100px;
}

.dialog-content {
  display: flex;
  width: 100%;
  padding: 10px;
  .dialog-main {
    flex-grow: 1;
    margin-right: 20px;
  }
  .dialog-aside {
    border-left: 1px solid #ccc;
    width: 200px;
  }
}

</style>
